package com.gem.service.impl;

import com.gem.domain.CompanyVisit;
import com.gem.mapper.CompanyVisitMapper;
import com.gem.service.CompanyVisitService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.NoTransactionException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Bravea❤ ·勇
 * Description: 企业拜访记录service
 * create
 * Modified By:
 */
@Service
@Transactional
public class CompanyVisitServiceImpl implements CompanyVisitService {

    @Autowired
    private CompanyVisitMapper companyVisitMapper;

    @Override
    public boolean addCompanyVisit(CompanyVisit c) {
        try {
            return companyVisitMapper.addCompanyVisit(c);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public CompanyVisit selectCompanyVisitById(String comVisitId) {
        try {
            return companyVisitMapper.selectCompanyVisitById(comVisitId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<CompanyVisit> selectCompanyVisitByCompanyId(int currentPage, int pageSize,String comVisitCompanyId) {
        PageInfo<CompanyVisit> pageInfo = null;
        try {
            PageHelper.startPage(currentPage, pageSize);
            pageInfo = new PageInfo<>(companyVisitMapper.selectCompanyVisitByCompanyId(comVisitCompanyId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public PageInfo<CompanyVisit> selectCompanyVisitEByCompanyId(int currentPage, int pageSize, String comVisitCompanyId) {
        PageInfo<CompanyVisit> pageInfo = null;
        try {
            PageHelper.startPage(currentPage, pageSize);
            pageInfo = new PageInfo<>(companyVisitMapper.selectCompanyVisitEByCompanyId(comVisitCompanyId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public PageInfo<CompanyVisit> selectCompanyVisitJByCompanyId(int currentPage, int pageSize, String comVisitCompanyId) {
        PageInfo<CompanyVisit> pageInfo = null;
        try {
            PageHelper.startPage(currentPage, pageSize);
            pageInfo = new PageInfo<>(companyVisitMapper.selectCompanyVisitJByCompanyId(comVisitCompanyId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public PageInfo<CompanyVisit> selectMyCompanyVisitByCompanyId(int currentPage, int pageSize, String comVisitCompanyId, String personname) {
        PageInfo<CompanyVisit> pageInfo = null;
        try {
            PageHelper.startPage(currentPage, pageSize);
            pageInfo = new PageInfo<>(companyVisitMapper.selectMyCompanyVisitByCompanyId(comVisitCompanyId,personname));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public boolean deleteCompanyVisitById(String[] comVisitId) {
        try {
            boolean isDeleteSuccess = true;
            for (String c: comVisitId) {
                if (c != null && !c.trim().equals("")){
                    if(!companyVisitMapper.deleteCompanyVisitById(c)){
                        isDeleteSuccess = false;
                        break;
                    }
                }
            }
            if(isDeleteSuccess){
                return true;
            }else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public boolean modifyCompanyVisitById(CompanyVisit c) {
        try {
            return companyVisitMapper.modifyCompanyVisitById(c);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<CompanyVisit> searchCompanyVisit(String comVisitCompanyName, String comVisitInitiator, Date comVisitDate) {
        try {
            comVisitCompanyName = "%"+ comVisitCompanyName +"%";
            comVisitInitiator = "%" + comVisitInitiator + "%";
            String str_comVisitDate = "";
            if (comVisitDate==null){
                str_comVisitDate="%%";
            }else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                str_comVisitDate = "%" + sdf.format(comVisitDate) + "%";
            }
            return companyVisitMapper.searchCompanyVisit(comVisitCompanyName,comVisitInitiator,str_comVisitDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<CompanyVisit> searchCompanyEVisit(String comVisitCompanyName, String comVisitInitiator, Date comVisitDate) {
        try {
            comVisitCompanyName = "%"+ comVisitCompanyName +"%";
            comVisitInitiator = "%" + comVisitInitiator + "%";
            String str_comVisitDate = "";
            if (comVisitDate==null){
                str_comVisitDate="%%";
            }else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                str_comVisitDate = "%" + sdf.format(comVisitDate) + "%";
            }
            return companyVisitMapper.searchCompanyEVisit(comVisitCompanyName,comVisitInitiator,str_comVisitDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<CompanyVisit> searchCompanyJVisit(String comVisitCompanyName, String comVisitInitiator, Date comVisitDate) {
        try {
            comVisitCompanyName = "%"+ comVisitCompanyName +"%";
            comVisitInitiator = "%" + comVisitInitiator + "%";
            String str_comVisitDate = "";
            if (comVisitDate==null){
                str_comVisitDate="%%";
            }else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                str_comVisitDate = "%" + sdf.format(comVisitDate) + "%";
            }
            return companyVisitMapper.searchCompanyjVisit(comVisitCompanyName,comVisitInitiator,str_comVisitDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<CompanyVisit> selectAllCompanyVisit() {
        try {
            return companyVisitMapper.selectAllCompanyVisit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<CompanyVisit> selectCompanyVisitByPage(int currentPage, int pageSize) {
        PageInfo<CompanyVisit> pageInfo = null;
        try {
            PageHelper.startPage(currentPage, pageSize);
            pageInfo = new PageInfo<>(companyVisitMapper.selectAllCompanyVisit());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public PageInfo<CompanyVisit> selectCompanyVisitEByPage(int currentPage, int pageSize) {
        PageInfo<CompanyVisit> pageInfo = null;
        try {
            PageHelper.startPage(currentPage, pageSize);
            pageInfo = new PageInfo<>(companyVisitMapper.selectCompanyVisitE());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public PageInfo<CompanyVisit> selectCompanyVisitJByPage(int currentPage, int pageSize) {
        PageInfo<CompanyVisit> pageInfo = null;
        try {
            PageHelper.startPage(currentPage, pageSize);
            pageInfo = new PageInfo<>(companyVisitMapper.selectCompanyVisitJ());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public PageInfo<CompanyVisit> selectMyCompanyVisitByPage(int currentPage, int pageSize, String personname) {
        PageInfo<CompanyVisit> pageInfo = null;
        try {
            PageHelper.startPage(currentPage, pageSize);
            pageInfo = new PageInfo<>(companyVisitMapper.selectMyCompanyVisit(personname));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public PageInfo<CompanyVisit> searchCompanyVisitByPage(int currentPage, int pageSize, String coName, String personname, Date comVisitDate) {
        PageHelper.startPage(currentPage, pageSize);
        PageInfo<CompanyVisit> pageInfo = null;
        try {
            pageInfo = new PageInfo<>(searchCompanyVisit(coName,personname,comVisitDate));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public PageInfo<CompanyVisit> searchCompanyVisitEByPage(Integer currentPage, Integer pageSize, String coName, String personname, Date comVisitDate) {
        PageHelper.startPage(currentPage, pageSize);
        PageInfo<CompanyVisit> pageInfo = null;
        try {
            pageInfo = new PageInfo<>(searchCompanyEVisit(coName,personname,comVisitDate));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public PageInfo<CompanyVisit> searchCompanyVisitJByPage(Integer currentPage, Integer pageSize, String coName, String personname, Date comVisitDate) {
        PageHelper.startPage(currentPage, pageSize);
        PageInfo<CompanyVisit> pageInfo = null;
        try {
            pageInfo = new PageInfo<>(searchCompanyJVisit(coName,personname,comVisitDate));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public boolean addCompanyVisitList(List<CompanyVisit> list) {
        try {
            boolean is = true;
            for (CompanyVisit visit : list) {
                if(!companyVisitMapper.addCompanyVisit(visit)){
                    is = false;
                }
            }
            if (is){
                return true;
            }else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (NoTransactionException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return false;
    }
}
